<template>
  <div>
    <!-- 经销商库存查询 -->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110"></TopFilter>

    <Spin :spinning="exportLoading" tip="Loading...">
      <FilterTable
        ref="table"
        :columns="columns"
        :fetchapi="fetchApi"
        :params="fetchParams"
        :datakey="'records'"
        :isSelectColumn="false"
        columnsRef="zjInventoryQuery"
        :onRowSelectChange="selectChangeHandle"
        :onColumnsChange="columns => (this.columns = columns)"
      >
        <template slot="controls" slot-scope="props">
          <multiuse-button size="small" type="primary" @click="addHandle()">导 出</multiuse-button>
        </template>
      </FilterTable>
    </Spin>
  </div>
</template>
<script>
import { getList, vehicleConfiguration, exportOem, getWarehouseList,exporturl } from '@/api/dealerSale/InventoryManagement/InventoryQuery/index.js';
import { dictionary } from '@/utils/dictMixin';
import { exportExcelFile } from '@/utils';
export default {
  name: 'TemplateDistribution',
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchParams: {
        isDealer: 1,
        isSelect: 10041001,
        xhrAbort: true
      },
      fetchApi: getList,
      exportLoading: false,
      formData: {},
      issearch: false,
      dealerCode: ''
    };
  },
  components: {},
  mixins: [dictionary],
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {
    this.getList();
  },
  methods: {
    createdFrom() {
      return [
        {
          type: 'TEXT_AREA',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: '请输入',
          maxlength: '',
          maxRows: 2
        },
        {
          type: 'SELECT',
          label: '仓库名称',
          fieldName: 'dlrWarehouseId',
          placeholder: '请选择',
          itemList: []
        },
        {
          type: 'MULTIPLE_SELECT',
          label: '车辆状态',
          fieldName: 'vehicleStatusList',
          placeholder: '请选择',
          itemList: [
            { text: '未发运', value: 60401007 },
            { text: '经销商在途', value: 60401002 },
            { text: '经销商库存', value: 60401003 },
            { text: '经销商已出库', value: 60401006 }
          ]
        },
        {
          type: 'INPUT_CASCADER',
          label: '车型选择',
          fieldName: 'vehicleInfo',
          placeholder: '请选择',
          options: { titles: ['品牌', '车系', '车型', '配置', '选装', '颜色'] },
          itemList: []
        },
        {
          type: 'INPUT',
          label: '产品名称',
          fieldName: 'productName',
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '产品代码',
          fieldName: 'productCode',
          placeholder: '请选择'
        },
        {
          type: 'SELECT',
          label: '是否锁定',
          fieldName: 'isLock',
          placeholder: '请选择',
          itemList: this.createDictList('1004')
        },
        {
          type: 'RANGE_DATE',
          label: '店端入库日期',
          fieldName: 'beginPostingDate|endPostingDate',
          placeholder: '请选择',
          initialValue: [],
          dateType: 'exactdaterange'
        },
        {
          type: 'SELECT',
          label: '配车状态',
          fieldName: 'distributionStatus',
          placeholder: '请选择',
          itemList: this.createDictList('6041')
        },
        {
          type: 'SELECT',
          label: '退车标记',
          fieldName: 'orderStatus',
          placeholder: '请选择',
          itemList: this.createDictList('1004')
        },
        {
          type: 'INPUT',
          label: '零售订单号',
          fieldName: 'orderNo',
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '销售订单号',
          fieldName: 'doNo',
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '客户姓名',
          fieldName: 'customerName',
          placeholder: '请输入'
        },
        {
          type: 'SELECT',
          label: '动力形式',
          fieldName: 'powerType',
          placeholder: '请选择',
          itemList: this.createDictList('6008')
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },

        {
          title: '车辆状态',
          dataIndex: 'vehicleStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: [
            { text: '未发运', value: 60401007 },
            { text: '经销商在途', value: 60401002 },
            { text: '经销商库存', value: 60401003 },
            { text: '经销商已出库', value: 60401006 }
          ],
          render: p => {
            return <span>{this.createDictText(p.row.vehicleStatus, '6040')}</span>;
          }
        },
        {
          title: 'VIN',
          dataIndex: 'vin',
          align: 'center',
          width: 180,
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '配车状态',
          dataIndex: 'distributionStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6041'),
          render: p => {
            return <span>{this.createDictText(p.row.distributionStatus, '6041')}</span>;
          }
        },
        {
          title: '是否锁定',
          dataIndex: 'isLock',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          render: p => {
            return <span>{this.createDictText(p.row.isLock, '1004')}</span>;
          }
        },
        {
          title: '锁定备注',
          dataIndex: 'lockRemark',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '是否总部冻结',
          dataIndex: 'isFreeze',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          render: p => {
            return <span>{this.createDictText(p.row.isFreeze, '1004')}</span>;
          }
        },
        {
          title: '冻结原因',
          dataIndex: 'freezeReason',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '冻结时间',
          dataIndex: 'freezeDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '仓库名称',
          dataIndex: 'dlrWarehouseId',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '动力形式',
          dataIndex: 'powerType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6008'),
          render: p => {
            return <span>{this.createDictText(p.row.powerType, '6008')}</span>;
          }
        },
        {
          title: '销售订单号',
          dataIndex: 'doNo',
          sorter: true,
          filter: true,
          filterType: 'input',
          align: 'center',
          width: 200
        },
        {
          title: '订单类型',
          dataIndex: 'orderType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6006'),
          render: p => {
            return <span>{this.createDictText(p.row.orderType, '6006')}</span>;
          }
        },
        {
          title: '是否三方车辆',
          dataIndex: 'isTripartite',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          render: p => {
            return <span>{this.createDictText(p.row.isTripartite, '1004')}</span>;
          }
        },
        {
          title: '三方票号',
          dataIndex: 'biilNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '订单开票价格',
          dataIndex: 'includeTaxAmount',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '折扣金额',
          dataIndex: 'discountAmount',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '折扣率(%)',
          dataIndex: 'discount',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '零售订单号',
          dataIndex: 'orderNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '客户名称',
          dataIndex: 'customerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '大客户项目名称',
          dataIndex: 'majorProjectName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车系名称',
          dataIndex: 'seriesName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '车型名称',
          dataIndex: 'modelName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配置名称',
          dataIndex: 'packageCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '选装名称',
          dataIndex: 'optionCode',
          align: 'center',
          width: 250,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '内饰',
          dataIndex: 'trimCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '外饰',
          dataIndex: 'colorCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '产品代码',
          dataIndex: 'productCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '产品名称',
          dataIndex: 'productName',
          align: 'center',
          width: 400,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车龄(天)',
          dataIndex: 'productDateAge',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '库龄(天)',
          dataIndex: 'receiveDateAge',
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '生产下线日期',
          dataIndex: 'productDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '销司出库日期',
          dataIndex: 'certificateDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '发车日期',
          dataIndex: 'chngDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '店端入库日期',
          dataIndex: 'receiveDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '店端配车日期',
          dataIndex: 'distributionDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '发动机号',
          dataIndex: 'engineNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '公告号',
          dataIndex: 'announceNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '合格证号',
          dataIndex: 'certificateNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '电机号',
          dataIndex: 'motorNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '电池号',
          dataIndex: 'batteryNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '退车标识',
          dataIndex: 'orderStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          render: p => {
            return <span>{this.createDictText(p.row.orderStatus, '1004')}</span>;
          }
        }
      ];
    },
    selectChangeHandle(row) {},
    async getList() {
      let res = await vehicleConfiguration(0, { isUnlimited: 10041001 });
      this.topFilterList.find(item => item.fieldName === 'vehicleInfo').itemList = res.data;
      let res1 = await getWarehouseList();

      this.topFilterList.find(item => item.fieldName === 'dlrWarehouseId').itemList = res1.data.list.map(x => ({ text: x.warehouseName, value: x.id }));
    },
    async addHandle(data) {
      let json = Object.assign({}, { CODE: 'CODE_0000048' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }

      // this.$confirm('是否将Excel表格保存至本地文件夹?', '提示', {
      //   confirmButtonText: '下载',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // })
      //   .then(() => {
      //     let json = this.fetchParams;
      //     this.exportLoading = true;

      //     exportOem(json)
      //       .then(response => {
      //         console.log(response, 'response');
      //         exportExcelFile(response.data, '经销商库存导出');
      //         this.exportLoading = false;
      //       })
      //       .catch(err => {
      //         console.log(err);
      //       });
      //   })
      //   .catch(() => {
      //     this.$message({
      //       type: 'info',
      //       message: '已取消'
      //     });
      //   });
    },
    changeHandle(params) {
      params.dealerCode = this.dealerCode;
      this.fetchParams.xhrAbort = false;

      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    editHandle(data) {
      this.emitvisible = true;
    },
    emitcloseHandler(val) {
      if (val) {
        console.log(val);
        this.topFilterList.map(x => {
          if (x.fieldName == 'dealerName') {
            x.initialValue = val.dealerName;
          }
        });
        this.dealerCode = val.dealerCode;
      }
      this.issearch = false;
    }
  }
};
</script>
